package net.huashitong.Indicators.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import net.huashitong.Indicators.entity.HotWord;
import net.huashitong.Indicators.service.HotWordsService;
import net.huashitong.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/hotword")
public class HotWordController extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return null;
    }

    @Autowired
    private HotWordsService service;

    /**
     * 保存热词
     */
    @ResponseBody
    @RequestMapping("/savehotwords")
    public void savaHotWord(){
        String areaParent = null;
        try {
            areaParent = Until.HttpGET(Until.HOT_WORDS_URL,5000,5000);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("jiekoiu:"+ Until.HOT_WORDS_URL);
        }
        JSONObject jsonObject1 =JSONObject.parseObject(areaParent);
        if("200".equals(jsonObject1.getJSONArray("returncode"))){
            JSONArray jsonArray = jsonObject1.getJSONArray("returndata");
            Object[] obArr = jsonArray.toArray();
            String[] strArr = new String[obArr.length];

            List<HotWord> hotWordList = new ArrayList<>();

            for(int  i =0;i<obArr.length;i++){
                HotWord hotWord = new HotWord();
                hotWord.setHotWord(obArr[i].toString());
                hotWordList.add(hotWord);
            }

            service.batchSave(hotWordList);
        }

    }
}
